package movie;

import movie.db.DBConfig;
import movie.ut.UTool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class MovieCatch {

	/**
	 * 抓取电影
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		int endPg = 112;
		for(int i=0; i < endPg; i++) {
			String u = "http://v.baidu.com/commonapi/movie2level/?callback=jQuery1910059014909989521414_1385356017290&filter=false&type=&area=&actor=&start=&complete=&order=rating&pn="+ 
					i +"&rating=&prop=&_=1385356017294";
			String ht = UTool.getHtml(u).trim();
			ht = ht.substring(0, ht.length() - 1); // 去掉尾部的 ')'
			ht = UTool.replace(ht, "jQuery\\d+\\_\\d+\\(", ""); // 去掉开头的 jQ 调用

			JSONObject jp = JSON.parseObject(ht);
			Integer total_num = jp.getInteger("total_num");
			JSONArray videos = jp.getJSONObject("videoshow").getJSONArray("videos");
			for (int idx= 0; idx < videos.size(); idx++) {
				save(videos.getJSONObject(idx));
			}
		}
	}

	static String tn = "mv";
	/**
	 * @param mv
	 */
	private static void save(JSONObject mv) {
		Record rc;
		rc = getRecord(mv);

//		Map<String, Object> kvs = rc.getColumns();
//		Set<Entry<String, Object>> ent = kvs.entrySet();
//		for (Entry<String, Object> e : ent) {
//			System.out.println(e.getKey() +" =>"+ e.getValue());
//		}
//		System.out.println("\n\n\n\n\n");
		
		Db.save(DBConfig.ds, tn, "id", rc);
	}
	private static Record getRecord(JSONObject mv) {
		Record rc = new Record();
		rc.set("actor", getAc(mv));
		rc.set("area", getAr(mv));
		rc.set("date", getDate(mv));
		rc.set("duration", getDur(mv));
		rc.set("extRd", getExt(mv));
		rc.set("hot", getHot(mv));
		rc.set("m_id", getId(mv)); // 原名是 id
		rc.set("imgh_url", getImghUrl(mv));
		rc.set("imgv_url", getImgvUrl(mv));
		rc.set("intro", getIntro(mv));
		rc.set("rating", getRat(mv));
		rc.set("s_intro", getSin(mv));
		rc.set("site", getSit(mv));
		rc.set("source", getSou(mv));
		rc.set("title", getTit(mv));
		rc.set("type", getTyp(mv));
		rc.set("url", getUr(mv));
		
		return rc;
	}
	
	//
	private static Object getUr(JSONObject mv) {
		return mv.getString("url").trim();
	}
	private static Object getTyp(JSONObject mv) {
		StringBuffer s = new StringBuffer();
		if (mv.getString("type").length() > 0) {
			JSONArray tp = mv.getJSONArray("type");
			for(int idx=0; idx < tp.size(); idx++) {
				JSONObject o = tp.getJSONObject(idx);
				s.append(o.getString("name") + " ");
			}
		}
		return s.toString().trim();
	}
	private static Object getTit(JSONObject mv) {
		return mv.getString("title").trim();
	}
	private static Object getSou(JSONObject mv) {
		return mv.getString("source").trim();
	}
	private static Object getSit(JSONObject mv) {
		return mv.getString("site").trim();
	}
	private static Object getSin(JSONObject mv) {
		return mv.getString("s_intro").trim();
	}
	private static Object getRat(JSONObject mv) {
		return mv.getString("rating").trim();
	}
	private static Object getIntro(JSONObject mv) {
		return mv.getString("intro").trim();
	}
	private static Object getImgvUrl(JSONObject mv) {
		return mv.getString("imgv_url").trim();
	}
	private static Object getImghUrl(JSONObject mv) {
		return mv.getString("imgh_url").trim();
	}
	private static Object getId(JSONObject mv) {
		return mv.getString("id").trim();
	}
	private static Object getHot(JSONObject mv) {
		return mv.getString("hot").trim();
	}
	private static String getExt(JSONObject mv) {
		StringBuffer s = new StringBuffer();
		try {
			if (mv.getString("extRd") != null
					&& mv.getString("extRd").length() > 0) {
				JSONArray ext = mv.getJSONArray("extRd");
				for(int idx=0; idx < ext.size(); idx++) {
					JSONObject o = ext.getJSONObject(idx);
					s.append(o.getString("name") + " ");
				}
			}
		} catch(Exception e) {
			System.out.println(mv);
		}
		return s.toString().trim();
	}
	private static String getDur(JSONObject mv) {
		return mv.getString("duration").trim();
	}
	private static String getDate(JSONObject mv) {
		return mv.getString("date").trim();
	}
	private static String getAr(JSONObject mv) {
		StringBuffer s = new StringBuffer();
		JSONArray ar = mv.getJSONArray("area");
		for(int idx=0; idx < ar.size(); idx++) {
			JSONObject o = ar.getJSONObject(idx);
			s.append(o.getString("name") + " ");
		}
		return s.toString().trim();
	}
	private static String getAc(JSONObject mv) {
		StringBuffer s = new StringBuffer();
		try{
			if (mv.getString("actor").length() > 0) {
				JSONArray ac = mv.getJSONArray("actor");
				if (ac.size() > 0)
					for(int idx=0; idx < ac.size(); idx++) {
						JSONObject o = ac.getJSONObject(idx);
						s.append(o.getString("name") + " ");
					}
			}
		} catch(Exception e) {
			System.out.println(mv);
		}
		return s.toString().trim();
	}

}
